package com.intramirror.wd.frontendAppserver;

import com.intramirror.framework.base.annotations.EnableFrameworkBaseView;
import com.intramirror.framework.client.annotations.EnableFrameworkClientFeign;
import com.intramirror.framework.config.annotations.EnableFrameworkConfigEurekaClient;
import io.github.xdiamond.client.XDiamondConfig;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.context.support.StandardServletEnvironment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@SpringBootApplication(scanBasePackages = { "com.intramirror.wd.front"})
//@EnableFrameworkConfigEurekaClient
//@EnableFrameworkClientFeign
//@EnableFrameworkBaseView
@EnableDiscoveryClient
@EnableFeignClients({"com.intramirror.wd.spi"})
@ComponentScan({"com.intramirror.wd.frontendAppserver"})
public class TestWebApplicaiton {

    @GetMapping("/api-docs")
    public String ApiDocs() {
        return "redirect:swagger-ui.html";
    }


    /**
     * 保持所有的配置文件
     * 某些场景可能存在动态变更配置情况，可以直接从XDiamondconfig中动态获取参数
     * */
    private static Map<String, XDiamondConfig> xDiamondConfigMap;


    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(TestWebApplicaiton.class);
        application.setEnvironment(initConfigCenterEnviroment());
        application.run(args);
    }

    private static ConfigurableEnvironment initConfigCenterEnviroment(){
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/config/xdiamondconfig.xml");
        ctx.start();
        //List<PropertiesPropertySource> propertiesPropertySources = getPropertiesPropertySourceFromXdiamond(ctx);
        List<PropertiesPropertySource> propertySources = new ArrayList<>();
        xDiamondConfigMap = ctx.getBeansOfType(XDiamondConfig.class);
        if (MapUtils.isNotEmpty(xDiamondConfigMap)) {
            xDiamondConfigMap.forEach((key, xDiamondConfig) -> propertySources.add(new PropertiesPropertySource(key, xDiamondConfig.getProperties())));
        }
        ctx.stop();

        StandardServletEnvironment environment = new StandardServletEnvironment() {
            @Override
            protected void customizePropertySources(MutablePropertySources mpropertySources) {
                if (CollectionUtils.isNotEmpty(propertySources)) {
                    propertySources.forEach(mpropertySources::addLast);
                }
                super.customizePropertySources(mpropertySources);
            }
        };

        return environment;
    }

    public static Map<String, XDiamondConfig> getxDiamondConfigMap(){
        return xDiamondConfigMap;
    }

}
